use bson::{to_bson, to_document, Document};
use serde::{Deserialize, Serialize};
use serde_json::{Result, Value};
mod single_thread_server;
use single_thread_server::run;
mod bytes_operation;
//mod file_test;
mod bson_operation;
mod minidb;
mod utils;
use crate::minidb::CollectionManager;
use std::env;

fn main() {
    let args: Vec<String> = env::args().collect();
    let collection_name = &args[1];
    let op_name = &args[2];
    let op_arg = &args[3];

    let mut co = CollectionManager::new(collection_name);
    co.open();

    if op_name == "load" {
        let bson_docs = bson_operation::load_jsons_in_directory(op_arg);
        let bson_bs = bson_operation::convert_bson_to_bytes(&bson_docs[..]);
        let docs = bson_operation::prepare_docs_for_insertion(&bson_bs);
        co.insert_docs(&docs);
    } else if op_name == "get" {
        let v: Vec<u64> = op_arg
            .split(" ")
            .map(|x| x.parse::<u64>().unwrap())
            .collect();
        let docs = co.get_doc_by_id(&v[..]);
        let docs = bson_operation::parse_documents(&docs);
        println!("Parsed: {:?}", docs);
    } else if op_name == "delete" {
        let v: Vec<u64> = op_arg
            .split(" ")
            .map(|x| x.parse::<u64>().unwrap())
            .collect();
        co.delete_doc_by_id(&v[..]);
    }
}
